<?php
if (!(defined('IN_IA'))) 
{
	exit('Access Denied');
}

//平台模型类
class Platform_MxShopModel {

    private $platformTable;
    private $platformSettingTable;

    public function __construct()
    {
        $this->platformTable = tablename('mx_shop_platform');
        $this->platformSettingTable = tablename('mx_shop_platform_setting');
    }

    /*
     * 获取平台设置
     * @param int $pfid 平台id
     * @return array()
     * */
    public function getSetting($pfid=0){

        if(empty($pfid)){
            return false;
        }

        return pdo_fetch('SELECT * FROM '.$this->platformSettingTable.' WHERE uniacid=:uniacid LIMIT 1', array(':uniacid'=>$pfid));
    }

    /*
     * 设置平台设置
     * @param int $pfid 平台id
     * @param string $type 配置类型
     * @return array()
     * */
    public function setSetting($pfid=0, $data=''){

        if(empty($pfid) || empty($data)){
            return false;
        }

        $setting = pdo_fetch('SELECT * FROM '.$this->platformSettingTable.' WHERE uniacid=:uniacid LIMIT 1', array(':uniacid'=>$pfid));

        //添加设置
        if(empty($setting)){
            $setting = array(
                'uniacid' => $pfid,
                'data' => serialize($data),
                'createtime' => TIMESTAMP,
            );
            pdo_insert('mx_shop_platform_setting', $setting);

            //更新设置
        }else{

            $update = array(
                'data' => serialize($data),
                'updatetime' => TIMESTAMP
            );

            pdo_update('mx_shop_platform_setting', $update, array('uniacid'=>$pfid));
        }
        return true;
    }

    /*
     * 获取本地平台列表
     *
     * */
    public function getList($pindex=1, $psize=20, $type=0)
    {
        $condition = '';
        if(!empty($type)){
            $condition .= ' AND status='.($type == 1 ? 1 : 0);
        }

        $pindex = max(1, $pindex);
        $psize = max(1, $psize);

        $limit = ' LIMIT '.($pindex-1) * $psize.','.$psize;

        $list = pdo_fetchall('SELECT * FROM '.$this->platformTable.' WHERE deleted=0'.$condition.$limit);

        $total = pdo_fetchcolumn('SELECT count(*) FROM '.$this->platformTable.' WHERE deleted=0'.$condition);

        return array('data'=>array('list'=>$list, 'total'=>$total));
    }

    /*
     * 获取本地平台详情 -》公众号id
     * @param int $uniacid 公众号id
     * @return array();
     * */
    public function getPlatformInfoByUniacid($uniacid)
    {
        if(empty($uniacid)){
            return null;
        }
        return pdo_fetch("SELECT id,platformid,name FROM " . $this->platformTable . " WHERE uniacid=:uniacid AND deleted=0 LIMIT 1", array(':uniacid' => $uniacid));
    }

    /*
     * 获取本地平台详情 -》平台id
     * @param int $platformid 平台id
     * @return array();
     * */
    public function getPlatformInfoByPlatformid($platformid)
    {
        if(empty($latformid)){
            return null;
        }
        return pdo_fetch("SELECT id,platformid,name FROM " . $this->platformTable . " WHERE platformid=:platformid AND deleted=0 LIMIT 1", array(':platformid' => $platformid));
    }

    /*
     * 添加平台
     * @param array() $data 内容
     * @return array();
     * */
    public function addPlatformToProxy($data=array())
    {
        if(empty($data['name']))
        {
            return null;
        }

        $params = array();

        //platfomr info
        $params['name'] = $data['name'];
        $params['description'] = empty($data['description']) ? '' : $data['description'];
        $params['appkey'] = empty($data['appkey']) ? '' : $data['appkey'];
        $params['appsecret'] = empty($data['appsecret']) ? '' : $data['appsecret'];

        //post appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //create sign
        $params['sign'] = create_sign($params);

        //request post
        $url = proxy_url('platform/add');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

    /*
     * 添加平台
     * @param int $pfid 平台唯一标识
     * @return array();
     * */
    public function getPlatformInfoToProxy($pfid=0)
    {
        if(empty($pfid))
        {
            return null;
        }

        $params = array();

        //platfomr id
        $params['platform'] = intval($pfid);

        //post appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //create sign
        $params['sign'] = create_sign($params);

        //request post
        $url = proxy_url('platform/add');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

    /*
     * 获取平台列表
     * @param int $pindex 页数
     * @param ing $psize 每页数据数
     * @param int $type 显示状态
     * @return array();
     * */
    public function getListToProxy($pindex=1, $psize=20, $type=0){

        $params = array();

        //状态：参数可选，0获取显示状态，1获取所有；默认0
        if(!empty($type)){
            $params['type'] = intval($type);
        }

        //页数：参数可选，默认1
        if(!empty($pindex)){
            $params['pindex'] = intval($pindex);
        }

        //每页数据数：参数可选，0获取显示状态，1获取所有；默认20
        if(!empty($psize)){
            $params['psize'] = intval($psize);
        }

        //timestamp and appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //sign
        $params['sign'] = create_sign($params);

        //send request post
        $url = proxy_url('platform/getlist');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

    /*
     * 修改平台状态
     * @param int $platformid 平台唯一标识
     * @param int $status 状态
     * @return array();
     * */
    public function editPlatformStatusToProxy($pfid, $status=1){

        if(empty($pfid)){
            return null;
        }

        $params = array();
        //platformid
        $params['platform'] = $pfid;

        //platfomr info
        $params['status'] = $status == 1 ? 1 : 0;

        //post appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //create sign
        $params['sign'] = create_sign($params);

        //send request post
        $url = proxy_url('platform/status');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

    /*
     * 修改平台信息
     * @param int $platformid 平台唯一标识
     * @param array() $data 修改的内容
     * @return array();
     * */
    public function editPlatformToProxy($pfid, $data=array()){

        if(empty($pfid) || empty($data['name'])){
            return null;
        }

        $params = array();
        //platformid
        $params['platform'] = $pfid;

        //platfomr info
        $params['name'] = $data['name'];
        $params['description'] = empty($data['description']) ? '' : $data['description'];
        $params['appkey'] = empty($data['appkey']) ? '' : $data['appkey'];
        $params['appsecret'] = empty($data['appsecret']) ? '' : $data['appsecret'];

        //post appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //create sign
        $params['sign'] = create_sign($params);

        //request post
        $url = proxy_url('platform/edit');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

    /*
     * 设置平台分润
     * @param int $platformid 平台唯一标识
     * @param array() $data 分润比例
     * @return array();
     * */
    public function setPlatformBonusToProxy($pfid, $data=array()){

        if(empty($pfid) || empty($data)){
            return null;
        }

        $params = array();
        //platformid
        $params['platform'] = $pfid;

        //platfomr bonus info
        $params['moneyman'] = empty($data['moneyman']) ? 0 : round($data['moneyman'], 2);
        $params['store'] = empty($data['store']) ? 0 : round($data['store'], 2);
        $params['country'] = empty($data['country']) ? 0 : round($data['country'], 2);
        $params['province'] = empty($data['province']) ? 0 : round($data['province'], 2);
        $params['city'] = empty($data['city']) ? 0 : round($data['city'], 2);
        $params['area'] = empty($data['area']) ? 0 : round($data['area'], 2);
        $params['cashback'] = empty($data['cashback']) ? 0 : round($data['cashback'], 2);
        $params['recommend'] = empty($data['recommend']) ? 0 : round($data['recommend'], 2);
        $params['recommend2'] = empty($data['recommend2']) ? 0 : round($data['recommend2'], 2);
        $params['recommend3'] = empty($data['recommend3']) ? 0 : round($data['recommend3'], 2);
        $params['channel'] = empty($data['channel']) ? 0 : round($data['channel'], 2);
        $params['supplier'] = empty($data['supplier']) ? 0 : round($data['supplier'], 2);
        $params['producer'] = empty($data['producer']) ? 0 : round($data['producer'], 2);
        $params['foundation'] = empty($data['foundation']) ? 0 : round($data['foundation'], 2);
        $params['company'] = empty($data['company']) ? 0 : round($data['company'], 2);
        $params['technology'] = empty($data['technology']) ? 0 : round($data['technology'], 2);

        //post appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //create sign
        $params['sign'] = create_sign($params);

        //request post
        $url = proxy_url('platform/setbonus');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

    /*
     * 获取平台分润信息
     * @param int $platformid 平台唯一标识
     * @return array();
     * */
    public function getPlatformBonusToProxy($pfid){

        if(empty($pfid)){
            return null;
        }

        $params = array();
        //platformid
        $params['platform'] = $pfid;

        //post appid
        $params['timestamp'] = TIMESTAMP;
        $params['appid'] = MX_SHOP_APPID;

        //create sign
        $params['sign'] = create_sign($params);

        //request post
        $url = proxy_url('platform/getbonus');
        $res = request_post($url, $params);
        $res = proxy_res_conversions($res);

        return $res;
    }

}
